package com.situ.mall.controller;

import com.situ.mall.pojo.entity.Admin;
import com.situ.mall.pojo.query.AdminQuery;
import com.situ.mall.service.IAdminService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/page")
public class PageController {

    @Autowired
    private IAdminService adminService;

    /**
     * /page/user/add     /page/login
     *
     * @return
     */
    @RequestMapping("/**")
    public String path(HttpServletRequest request) {

//        HttpSession session = request.getSession();
//        Admin admin = (Admin) session.getAttribute("admin");
//
//        if (ObjectUtils.isEmpty(admin)) {
//            Cookie[] cookies = request.getCookies();
//            Cookie cookie = null;
//            for (Cookie c : cookies) {
//                String name = c.getName();
//                if ("autoLogin".equals(name)) {
//                    cookie = c;
//                    break;
//                }
//            }
//            if (!ObjectUtils.isEmpty(cookie)) {
//                String[] values = cookie.getValue().split("&");
//                AdminQuery adminQuery = new AdminQuery();
//                admin.setName(values[0]);
//                admin.setPassword(values[1]);
//                Admin login = adminService.login(adminQuery.getAccount(), adminQuery.getPassword());
//                if (!ObjectUtils.isEmpty(login)) {
//                    session.setAttribute("admin", login);
//                }
//            }
//
//        }

        String requestURI = request.getRequestURI();
        System.out.println("requestURI: " + requestURI);
        String[] paths = requestURI.split("/");
        //["","page","user","list"]
        //["","page","login"]
        if (paths.length == 4) {
            return paths[2] + "_" + paths[3];
        } else if (paths.length == 3) {
            return paths[2];
        } else {
            return "index";
        }
    }
}
